﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmTaoTaiKhoan : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        private int idTK;
        private string _id;
        tblTaikhoan tbltk = new tblTaikhoan();
        tblHocsinh_giaovien hs_gv = new tblHocsinh_giaovien();
        MD5Hash md5hash = new MD5Hash();
        public frmTaoTaiKhoan(frmMain frm)
        {
            InitializeComponent();
            frmM = frm;
            idTK = -1;
            gridlookupEdit();
        }

        private void frmTaoTaiKhoan_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (idTK == -1)
                frmM.frmTaoTaiKhoan_isShown = false;
            
        }
        public void gridlookupEdit()
        {
            DataTable dthssv = hs_gv.Selecthocsinh_giaovien();
            dthssv.Columns.Add("STT");
            for (int i = 0; i < dthssv.Rows.Count; i++)
                dthssv.Rows[i]["STT"] = i + 1;
            gridLookUpEditHS_GV.Properties.DataSource = dthssv;
            gridLookUpEditHS_GV.Properties.DisplayMember = "hoten";
            gridLookUpEditHS_GV.Properties.ValueMember = "id";
        }
        private string checkTaikhoan(string u)
        {
            DataTable dt = new DataTable();
            dt = tbltk.SelectTaikhoan(u);
            if (dt.Rows.Count > 0)
            {
                DataRow r = dt.Rows[0];
                return r["taikhoan"].ToString().Trim();
            }
            else return "";
            
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtTaikhoan.Text == "" || txtMatkhau.Text == "" || txtNhaclaiMK.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa nhập đầy đủ thông tin", "Xác nhận", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {

                if (checkTaikhoan(txtTaikhoan.Text) != "")
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Tài khoản tồn tại", "Xác nhận", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMatkhau.Text = "";
                    txtNhaclaiMK.Text = "";
                    txtTaikhoan.Text = "";
                }
                else
                {
                    if (txtMatkhau.Text != txtNhaclaiMK.Text)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xác nhận lại mật khẩu", "Xác nhận", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtMatkhau.Text = "";
                        txtNhaclaiMK.Text = "";
                    }
                    else
                    {
                        int temp = Int16.Parse(_id);
                        string _MKTK = md5hash.CalculateMD5Hash(txtMatkhau.Text);
                        //insert tài khoản
                        tbltk.Insert(txtTaikhoan.Text, _MKTK, temp, true, 1);
                        frmM.GridViewTaiKhoan();
                        gridlookupEdit();
                        txtTaikhoan.Text = "";
                        txtNhaclaiMK.Text = "";
                        txtMatkhau.Text = "";
                    }
                }
                
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void gridLookUpnguoidung_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                this._id = this.gridLookUpnguoidung.GetRowCellValue(e.RowHandle, "id").ToString().Trim();

            }
            else
            {
                this._id = "";

            }
        }

        private void btnLuuVaThoat_Click(object sender, EventArgs e)
        {
            btnSave_Click(sender, e);
            btnExit_Click(sender, e);
        }
    }
}